package com.eleven.mall.controller;

import com.eleven.mall.mapper.GoodsCatsMapper;
import com.eleven.mall.pojo.GoodsCatsPage;
import com.eleven.mall.pojo.Rbody;
import com.eleven.mall.pojo.entity.Category;
import com.eleven.mall.pojo.entity.GoodsCats;
import com.eleven.mall.service.GoodsCatsService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@CrossOrigin
public class GoodsCatsController {

    GoodsCatsService goodsCatsService;

    //@GetMapping("/categories")
    public Rbody getGoodsCatsList(String query, Integer pagenum, Integer pagesize){
        System.out.println("==========查询参数:"+query+" pagenum:"+pagenum+" pagesize:"+pagesize);
        Rbody rbody = new Rbody();
        GoodsCatsPage goodsCatsPage = goodsCatsService.getGoodsCatsList(query, pagenum, pagesize);

        rbody.setData(goodsCatsPage);
        rbody.getMeta().setMsg("获取成功");
        return rbody;
    }

    @PostMapping("/categories")
    public Rbody addUser(@RequestBody GoodsCats goodsCats){
        System.out.println(goodsCats);
        Category category = goodsCatsService.addCategory(goodsCats);
        Rbody rbody = new Rbody();
        if (category == null){
            rbody.getMeta().setStatus(500);
            rbody.getMeta().setMsg("创建分类失败");
        }else{
            rbody.getMeta().setStatus(201);
            rbody.getMeta().setMsg("创建分类成功");
            rbody.setData(category);
        }
        return rbody;
    }
}
